import { MobileEventMixins } from '@/views/youshuda/base/mobile/mixins/MobileEventMixins'
import { FromBuildConfigMixins } from '@/views/youshuda/mixins/FromBuildConfigMixins'

export const PageMixins = {
  components: {
    //页面配置展示组件
    DraggableItem: () => import('@/views/youshuda/components/FormBuild/generator/DraggableItem')
  },
  mixins: [MobileEventMixins,FromBuildConfigMixins],
  props: {
    //页面设计Key
    formBuildKey: {
      default: ''
    }
  },
  computed: {
  },
  data() {
    return {
      //表单设计字段配置信息
      pageBuildConfig: [],
      //页面配置
      pageConfig:{},
      // 标识进入页面是否加载完成
      isLoadFinish: false,
      // 加载图片
      loading: require('@/assets/images/loading.gif'),
      //重新渲染组件时唯一标识
      nowRenderKey: '',
      //页面表单数据
      pageFormData:{},
      //历史加载标识
      pageKeyHistory: '',
      pageDict:{},
    }
  },
  created() {
    this.loading = this.loading + '?' + Math.random()
    /** 初始化页面 **/
    // if(!this.formBuildKey){
    //  this.initPage();
    // }
    // console.log('页面初始化',this.formBuildKey)
  },
  /** 当前页面销毁前，事件处理 **/
  beforeDestroy() {
    if (this.pageConfig && this.pageConfig['destroyMethod']) {
      this.evalEventCode(this.pageConfig['destroyMethod'])
    }
  },
  watch: {
    'formBuildKey':{
      immediate:true,
      handler(val){
        if(val){
          this.$nextTick(()=>{
            if(this.pageKeyHistory == val){
              return;
            }
            this.pageKeyHistory = val;
            this.initPage()
          })

        }
      }
    },
    '$route.params.pageKey':{
      immediate:true,
      handler(val){
        if(val){
          this.initPage();
          this.pageKeyHistory = val;
        }
      }
    }
  },
  methods:{
    /**
     * 初始化页面
     */
    initPage(){
      let _formBuildKey = this.formBuildKey;
      if(!_formBuildKey){
        _formBuildKey = this.$route.params.pageKey
      }
      if(!_formBuildKey){
        this.$message({
          message: '获取页面唯一标记失败！',
          type: 'error'
        })
        return;
      }
      let processNodeForm = {
        'formBuildKey': _formBuildKey,
        'clientType': 'Mobile',
        't': new Date().getTime()
      }
      this.isLoadFinish = false;
      // this.openLoading()
      this.request({
        url: '/common/basemode/mobile/getFormBuildConfig',
        method: 'post',
        data: processNodeForm
      }).then(res => {
        if (res.code == 200) {
          if (res.data) {
            let _pageBuildConfig = res.data['pageBuildConfig']
            if(_pageBuildConfig['formBuildInfo']){
              // _pageBuildConfig['formBuildInfo'] = this.dealDictBuildConfig(res.data['pageDictData'],_pageBuildConfig['formBuildInfo'])
              this.pageBuildConfig = this.dealFormBuildConfig(null,null,_pageBuildConfig['formBuildInfo']);
              this.pageConfig = _pageBuildConfig['pageConfig']
              this.isLoadFinish = true;
              this.dealFormLoadFinishEvent();
            }else{
              this.$message({
                message: '获取页面配置失败！',
                type: 'error'
              })
            }
          }else{
            this.$message({
              message: '获取页面失败！',
              type: 'error'
            })
          }
        }else{
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      }).finally(() => {
        // this.closeLoading()
      })
    },
    /** 页面初始化完成后，页面初始化方法 **/
    dealFormLoadFinishEvent(){
      if(this.pageConfig && this.pageConfig['initMethod']){
        this.evalEventCode(this.pageConfig['initMethod'])
      }
    },
    /**
     * 页面输入框值改变时，触发事件
     */
    changeFormData(item){
      if (item == null || item == undefined) {
        return
      }
      let fieldName = item.key;
      /**
       * 下拉框联动的情况
       */
      if(!this.pageFormData[item.parentName]){
        this.pageFormData[item.parentName] = {}
      }
      if(item.parentName){
        this.pageFormData[item.parentName][fieldName] = item.val
      }else{
        this.pageFormData[fieldName] = item.val
      }
      this.changeFieldConfigValue(fieldName, 'fieldValue', item.val)
    },
    /** 将字典信息放入对应组件中，设置为不可编辑状态 **/
    dealDictBuildConfig(pageDict,formBuildConfigs){
      function findColumn(formBuildConfig){
        if(Array.isArray(formBuildConfigs)){
          for (let field of formBuildConfig) {
            field['dictData'] = pageDict[field.vModel]
            if(field && field.children && Array.isArray(field.children) && field.children.length>0){
              field.children = findColumn(field.children)
            }
          }
        }
        return formBuildConfig;
      }
      return findColumn(formBuildConfigs)
    }
  }
}
